import 'dart:async';

import 'package:flutter/material.dart';
import 'package:jdapp/widget/JdText.dart';
import 'package:jdapp/widget/JdButton.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jdapp/config/jj_domain.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:dio/dio.dart';

class RegisterSecondPage extends StatefulWidget {
  Map arguments;
  RegisterSecondPage({Key? key, required this.arguments}) : super(key: key);
  _RegisterSecondPageState createState() => _RegisterSecondPageState();
}

class _RegisterSecondPageState extends State<RegisterSecondPage> {
  String tel = "null";
  var sendCodeBtn = false;
  int seconds = 5;
  String code = "";
  initState() {
    super.initState();
    this.tel = widget.arguments["tel"];
    _showTimer();
  }

  _showTimer() {
    var t;
    // Timer t;用这个下面t引用不到
    t = Timer.periodic(Duration(milliseconds: 1000), (Timer) {
      setState(() {
        seconds--;
      });
      if (this.seconds == 0) {
        this.sendCodeBtn = true;
        t.cancel(); //清除定时器
      }
    });
  }

  sendCode() async {
    var api = "${JJConfig.domain}api/sendCode";
    var response = await Dio().post(api, data: {"tel": this.tel});
    if (response.data["success"]) {
      setState(() {
        sendCodeBtn = false;
        seconds = 5;
        _showTimer();
      });
    } else {
      Fluttertoast.showToast(
        msg: '${response.data["message"]}',
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
      );
      setState(() {
        sendCodeBtn = false;
        seconds = 5;
        _showTimer();
      });
    }
  }

//验证验证码
  validateCode() async {
    var api = "${JJConfig.domain}api/validateCode";
    var response =
        await Dio().post(api, data: {"tel": this.tel, "code": this.code});
    if (response.data["success"]) {
      Navigator.pushNamed(context, '/registerThird');
    } else {
      Fluttertoast.showToast(
        msg: '${response.data["message"]}',
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
      );
      Navigator.pushNamed(context, '/registerThird',
          arguments: {"tel": tel, "code": code});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("用户注册-第二步"),
      ),
      body: Container(
        padding: EdgeInsets.all(20),
        child: ListView(
          children: <Widget>[
            SizedBox(height: 50),
            Container(
              padding: EdgeInsets.only(left: 10),
              child: Text("请输入${tel}手机收到的验证码,请输入${tel}手机收到的验证码"),
            ),
            SizedBox(height: 40),
            Stack(
              children: <Widget>[
                JdText(
                  text: "请输入验证码",
                  onChanged: (value) {
                    this.code = value;
                  },
                ),
                Positioned(
                  right: 0,
                  top: 0,
                  child: sendCodeBtn
                      ? RaisedButton(
                          child: Text('重新发送'),
                          onPressed: sendCode,
                        )
                      : RaisedButton(
                          child: Text('${seconds}'),
                          onPressed: () {},
                        ),
                )
              ],
            ),
            SizedBox(height: 20),
            JdButton(
              text: "下一步",
              color: Colors.red,
              //height: 74,
              cb: () {
                Navigator.pushNamed(context, '/registerThird', arguments: {"code":code,"tel":tel});
              },
            )
          ],
        ),
      ),
    );
  }
}
